import dayjs from 'dayjs'
import quarterOfYear from 'dayjs/plugin/quarterOfYear'

dayjs.extend(quarterOfYear)

export function uuid(prefix = '') {
  return prefix + (
    Date.now().toString(32)
    + Math.random().toString(32).substring(2)
  ).substring(2, 18)
}

// 获取当前季度
export function getQuarter(): number {
  const month = new Date().getMonth() + 1
  return Math.ceil(month / 3)
}

export function plus(a: any, b: any) {
  const isEmpty = (x: any) => x == null || x === ''
  if (isEmpty(a) || isEmpty(b))
    return ''
  const n = +(+a + +b).toFixed(2)
  return Number.isFinite(n) ? n : ''
}

// 随机生成时间
export function randTime() {
  const now = dayjs()
  const num = Math.ceil(Math.random() * 10)
  return now.subtract(num, 'day').format('YYYY-MM-DD HH:mm:ss')
}

export { dayjs }
